import { getUsername } from '@/utils/functions/getUsername';
import { Logger } from '@/utils/logger';
import { TaskBase } from '../TaskBase';

export class TaskSign extends TaskBase {
    shouldRun(creep: Creep) {
        return 'structureType' in this.target &&
            this.target.structureType === STRUCTURE_CONTROLLER;
    }
    _run(creep: Creep) {
        const controller = <StructureController>this.target;
        const text = this.taskData.info;
        const result = creep.signController(controller, text);
        if (result === OK) {
            if (controller.owner || controller.reservation?.username !== getUsername()) {
                Logger.warn(`${creep.mark} 对一个非中立控制器进行了签名，房间 ${controller.pos.roomName}`, 'task', true);
            }
            return OK;
        }
        else if (result === ERR_NOT_IN_RANGE) {
            return ERR_NOT_IN_RANGE;
        }
        else {
            return OK;
        }
    }
}